package org.ljx.taobao.dao.impl;

import org.ljx.taobao.api.entity.Shopping;
import org.ljx.taobao.api.entity.User;

import org.ljx.taobao.dao.IUserDao;
import org.ljx.taobao.util.DruidUtil;

import java.sql.*;

/**
 * @Author: Ljx
 * @Date: 2021/12/31 11:21
 * @role: 用户dao层实现
 */
public class UserDaoImpl implements IUserDao {
    private final Connection conn = DruidUtil.getConn();

    /**
     * 注册用户
     *
     * @param user 用户实体
     * @return boolean
     * @throws SQLException sql语句异常
     */
    @Override
    public boolean insertUser(User user) throws SQLException {
        boolean x = true;

        String sql = "insert into user values(null,?,?,?)";
        String sql1 = "select * from user";
        java.sql.Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(sql1);
        while (rs.next()) {
            String name = rs.getString("uername");
            if (name.equals(user.getUserName())) {
                x = false;
            }
        }
        if (x) {
            PreparedStatement pst = null;
            pst = conn.prepareStatement(sql);
            pst.setString(1, user.getUserName());
            pst.setString(2, user.getUserPassword());
            pst.setString(3, user.getPhone());
            int len = pst.executeUpdate();
            if (len > 0) {
                return true;
            }
        }
        conn.close();
        return false;
    }

    /**
     * 修改用户密码
     *
     * @param user 用户实体
     * @return boolean
     * @throws ClassNotFoundException 文件异常
     */
    @Override
    public boolean change(User user) throws ClassNotFoundException {

        try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/runoob", "root", "asd.123");
            String sql = "select * from user";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                String name = rs.getString("uername");
                String phone = rs.getString("phone");
                if (name.equals(user.getUserName()) && phone.equals(user.getPhone())) {
                    String sql1 = "UPDATE user SET PASSWORD=? WHERE uername=?";
                    PreparedStatement ps = conn.prepareStatement(sql1);
                    ps.setString(1, user.getUserPassword());
                    ps.setString(2, user.getUserName());
                    ps.execute();
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static int uid;

    /**
     * 用户登录
     *
     * @param user 用户实体
     * @return boolean
     */
    @Override
    public boolean login(User user) {
        try {
            Shopping shopping = new Shopping();
            String sql = "select * from user";
            java.sql.Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                String paw = rs.getString("password");
                String name = rs.getString("uername");
                int id = rs.getInt("id");
                if (paw.equals(user.getUserPassword()) && name.equals(user.getUserName())) {
                    uid = id;
                    return true;
                }
            }
            rs.close();
            st.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 删除用户
     *
     * @param user 用户实体
     * @return boolean
     */
    @Override
    public boolean delete(User user) {
        try {


            String sql = "delete from user where uername=?";
            String sql1 = "select * from user";
            String sql2 = "delete from shopping where uid=?";
            java.sql.Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql1);
            PreparedStatement pr = conn.prepareStatement(sql);
            PreparedStatement pr1 = conn.prepareStatement(sql2);
            while (rs.next()) {
                String name = rs.getString("uername");
                String password = rs.getString("password");
                if (name.equals(user.getUserName()) && password.equals(user.getUserPassword())) {
                    pr.setString(1, name);
                    pr1.setInt(1, uid);
                    pr.execute();
                    pr1.execute();
                    return true;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
